package com.app114.app.android.rk.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app114.app.android.rk.Browser;
import com.app114.app.android.rk.CategoryListActivity;
import com.app114.app.android.rk.R;
import com.app114.app.android.rk.utils.DataFactory;

public class CategoryAdapter extends ArrayAdapter<String> implements OnItemClickListener {

	private String[] mData;
	private Context mContext;
	private static final int mBACKGROUNDS[] = { R.drawable.sp_home_category_bg_cheng, R.drawable.sp_home_category_bg_lan,
			R.drawable.sp_home_category_bg_lv, R.drawable.sp_home_category_bg_zi };

	public CategoryAdapter(Context context, String[] data, AbsListView listView) {
		super(context, 0, data);
		this.mData = data;
		mContext = context;
		listView.setOnItemClickListener(this);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_main_category_item, null);
			holder = new Holder();
			holder.icon = (ImageView) convertView.findViewById(R.id.item_icon);
			holder.title = (TextView) convertView.findViewById(R.id.item_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		convertView.setBackgroundResource(mBACKGROUNDS[(int) (mBACKGROUNDS.length * Math.random())]);

		// Load the image and set it on the ImageView
		holder.title.setText(mData[position]);
		return convertView;
	}

	private static class Holder {
		TextView title;
		ImageView icon;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//		if (position == 5) {
//			Intent intent = new Intent(mContext, Browser.class);
//			intent.setAction(Intent.ACTION_VIEW);
//			intent.setData(Uri.parse(DataFactory.getWebSiteList(mData[position]).get(0).getGUrl()));
//			mContext.startActivity(intent);
//		} else {
			Intent intent = new Intent(mContext, CategoryListActivity.class);
			intent.setAction(mData[position]);
			mContext.startActivity(intent);
//		}
	}
}